 
/*  wreset.c
    used by tm40.c
    Stephen D. Anderson --- January 6, 1995

    Function resets the "configuration changed" flag and then
    resets field instrument.
    It does the following in order:

        Checks whether field instrum exists, does search if not.

        Sends command to reset "configuration changed" flag.

        Shows errors if any.

        Sends command to reset field instrument.

        Shows errors if any.

    ON ENTRY:   no arguments.

    ON EXIT:    nothing is returned.

    REVISION LOG:


*/

#include "tm40.h"

void wreset()
{
    typedef unsigned char byte;

    extern byte status1,status2;
    extern byte addr[];     /*  info needed to talk to field instrum. */
    extern byte found;     /*  indicates field instrument has been found. */
    extern byte search(void);    
    extern void err(byte);
    extern void err1(byte);
    extern int hartcmd(byte *, byte, byte *, byte, byte *, byte);

    int i,j;
    byte junk[1];

    /*  need field intr info? */
    if (!found) found = search();
    if (!found) return;

    /*  send command to clear the "configuration changed" flag.  */
    Wopen(5,12,12,68,2,WHITE|_BROWN,WHITE|_BROWN);
    WputCen("\n\nResetting Config Changed Flag");
    delay(1000);

    /*  send it.   */
    i = hartcmd(junk, 0, junk, 0, addr, 38);

    /*  command succeed?  */
    if ((i & 1) != 0)
    {
        /*  didn't succeed.  */
        delay(1000); err1(38); Wclose(); return;
    }

    /*  correct number of received bytes?  */
    if ((i/256) != 0)
    {
        /*  command didn't succeed.  */
        err(38); Wclose(); return;
    }

    /*  report success or error.  */
    Wclear();
    if (status1 != 0)
    {
        /*  error.  */
        err(38);
        WputCen("\n\nConfig Changed Bit");
        WputCen("\nmay not have been reset.");
    }
    else
    {
        /*  success.  */
        WputCen("\n\nReset Config Changed Sent");
    }
    WputCen("\n\nHit any key to resume");
    KeyWait();


    /*  try to reset field instrument. */
    Wclear();
    WputCen("\n\nResetting Field Instrument");
    delay(1000);

    /*  send it. */
    i = hartcmd(junk, 0, junk, 0, addr, 42);

    /*  command succeed?  */
    if ((i & 1) != 0)
    {
        /*  didn't succeed. */
        delay(1000); err1(42); Wclose(); return;
    }

    /*  correct number of received bytes?  */
    if ((i/256) != 0)
    {
        /*  command didn't succeed.  */
        err(42); Wclose(); return;
    }

    /*  report success or error.  */
    Wclear();
    if (status1 != 0)
    {
        /*  error.  */
        err(42);
        WputCen("\n\nField Instrum may");
        WputCen("\nnot have been reset.");
    }
    else
    {
        /*  success.  */
        WputCen("\n\nReset Command Sent");
    }
    WputCen("\n\nHit any key to resume");
    KeyWait(); Wclose(); return;
}

